<?php

namespace App\Http\Controllers\Client\Common;

use App\Http\Controllers\ClientController;
use Illuminate\Support\Facades\DB;

class HistoryLotteryBeforeController extends ClientController
{

    public function index()
    {
        // 发布过霸王餐活动的店铺、奖品信息
        $activities = DB::table('lottery_activity as la')
            ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
            ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
            ->where('la.creator', '=', $this->we_id)
            ->select('la.shop_id', 'la.goods_id', 'shop.shop_name', 'lg.goods_name')
            ->distinct('goods_id')
            ->get()
            ->toArray();
        $activities = json_decode(json_encode($activities), true);
        $shops = [];
        foreach($activities as $activity){
            if(isset($shops[$activity['shop_id']])){
                $shops[$activity['shop_id']]['goods'][] = [
                    'value'=>$activity['goods_id'],
                    'text'=>$activity['goods_name'],
                ];
            } else {
                $shops[$activity['shop_id']] = [
                    'value'=>$activity['shop_id'],
                    'text'=>$activity['shop_name'],
                    'goods' =>[[
                        'value'=>$activity['goods_id'],
                        'text'=>$activity['goods_name'],
                    ]],
                ];
            }
        }
        return $this->setReturnJson(0,'success',array_values($shops));

    }

}
